//
//  HotViewController.m
//  艾听歌
//
//  Created by dllo on 16/5/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "HotViewController.h"
#import "HotModel.h"
#import "HOTCOLLECTIONCell.h"
#import "HotDetailViewController.h"

#define HOTCELL @"hotcell"

@interface HotViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *HotcollectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *HotFlowLayout;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) BOOL isUpLoad;
@property (nonatomic, assign) NSInteger nextPage;

@end

@implementation HotViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    self.dataArray = [NSMutableArray array];
    [self getHandleData:self.nextPage];
    [self createCollectionView];
    
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorPickerWithRGB(0xFF6EB4, 0x9C9C9C);
    
    [self addHeader];
    [self addFooter];
    
    
}

- (void)addHeader
{
    __block HotViewController *root = self;
    self.HotcollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        root.nextPage = 1;
        root.isUpLoad = NO;
        [root getHandleData:root.nextPage];
        
    }];
    [self.HotcollectionView.mj_header beginRefreshing];
}
- (void)addFooter
{
    __block HotViewController *root = self;
    self.HotcollectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        root.isUpLoad = YES;
        root.nextPage++;
        [root getHandleData:root.nextPage];
        
    }];
}


- (void)getHandleData:(NSInteger)nextCursor
{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *str = [NSString stringWithFormat:@"http://search.dongting.com/songlist/search?q=tag:最热&page=%ld&size=10&app=ttpod&v=v9.0.1.2016050614&user_id=0&mid=iPhone7%%2C2&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=5e6106cc8d5bcf35f60a2997c79a00bb5a7c631f&idfa=ABDADD65-0877-4840-B14F-DFC8D1114AB0&utdid=VzR0dwnWYs8DANbYJ8SsFzOU&alf=(null)&bundle_id=com.ttpod.music&latitude=-1&longtitude=-1&access_token=(null)&vc=9000100", nextCursor];
    
    
    NSString *newStr = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    
    
    [manager GET:newStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [netWorkManager stopMonitoring];
        if (self.isUpLoad == NO) {
            [self.dataArray removeAllObjects];
        }
        NSMutableArray *bigArray = [responseObject objectForKey:@"data"];
        for (NSDictionary *dic in bigArray) {
            HotModel *hotModel = [[HotModel alloc] init];
            [hotModel setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:hotModel];
        }
        
        if (nextCursor == 1) {
            [self.dataArray removeObjectAtIndex:0];
        }
        [self.HotcollectionView.mj_footer endRefreshing];
        [self.HotcollectionView.mj_header endRefreshing];
        

        [self.HotcollectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}
- (void)createCollectionView
{
    //layout
    self.HotFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.HotFlowLayout.minimumInteritemSpacing = 0;
    self.HotFlowLayout.minimumLineSpacing = 10;
    self.HotFlowLayout.sectionInset = UIEdgeInsetsMake(20, 5, 0, 15);
    CGFloat w = (self.view.frame.size.width - 20 - 40) / 2 - 0.001;
    self.HotFlowLayout.itemSize = CGSizeMake(w, 200);
    
    //cv
    self.HotcollectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:self.HotFlowLayout];

    self.HotcollectionView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    self.HotcollectionView.delegate = self;
    self.HotcollectionView.dataSource = self;
    [self.view addSubview:self.HotcollectionView];
   // 注册
  [self.HotcollectionView registerClass:[HOTCOLLECTIONCell class] forCellWithReuseIdentifier:HOTCELL];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HOTCOLLECTIONCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HOTCELL forIndexPath:indexPath];
    HotModel *model = self.dataArray[indexPath.item];

    cell.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    [cell.pic sd_setImageWithURL:[NSURL URLWithString:model.pic_url] placeholderImage:[UIImage imageNamed:@"hold.png"]];
    cell.namelabel.text = model.author;
    cell.songLabel.text = model.title;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotDetailViewController *hotVC = [[HotDetailViewController alloc] init];

    HotModel *model = self.dataArray[indexPath.row];
    hotVC._id = model._id;
    
    
    
    [self.navigationController pushViewController:hotVC animated:YES];
}
// 视图将要出现的时候 tabBar不隐藏
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
  }


@end
